package com.example.springbootblog.controller;


import com.example.springbootblog.model.BlogInfo;
import com.example.springbootblog.model.Result;
import com.example.springbootblog.model.UserInfo;
import com.example.springbootblog.service.BlogService;
import com.example.springbootblog.service.UserInfoService;
import com.example.springbootblog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/blog")
@RestController
@Slf4j
public class BlogController {

    /**
     * 获取博客列表
     */
    @Autowired
    BlogService blogService;
    @Autowired
    UserInfoService userInfoService;

    @RequestMapping("/getList")
    public List<BlogInfo> getBlogList() {
        log.info("获取博客列表");
        return blogService.getBlogList();
    }


    /**
     * 获取博客详情
     */
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogById(Integer blogId,HttpServletRequest request) {
        log.info("获取博客详情");
        BlogInfo blogInfo = blogService.getBlogById(blogId);
        if(blogInfo == null) {
            return null;
        }
        // 获取登入用户id
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if(userId == blogInfo.getUserId()) {
            // 作者和登入用户是同一个
            blogInfo.setLoginUser(true);
        }

        return blogInfo;
    }

    /**
     * 添加博客
     */
    @RequestMapping("/add")
    public Result add(String title, String content, HttpServletRequest request) {
        log.info("title: " + title + " content: " + content);
        if(!StringUtils.hasLength(title) || !StringUtils.hasLength(content)) {
            return  Result.fail("添加失败,请联系管理员",false);
        }
        // 从 token 获取 userId

        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(token);
        if(userId == null || userId <= 0) {
            log.error("获取userId失败 userId: " + userId);
            return Result.fail("用户未登入",false);
        }
        // 构建 BlogInfo 对象
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setTitle(title);
        blogInfo.setContent(content);
        blogInfo.setUserId(userId);

        Integer result = blogService.add(blogInfo);
        if(result <= 0) {
            log.error("添加失败 result: " + result);
            return Result.fail("添加失败,请联系管理员",false);
        }
        return Result.success(true);
    }
    @RequestMapping("/update")
    public Result update(BlogInfo blogInfo,HttpServletRequest request) {

        // 获取 用户 id
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(token);
        log.info("userId: " + userId + " blogInfo.getUserId: " + blogInfo.getUserId());
        // 根据博客id获取到作者id

        UserInfo authorInfo = userInfoService.getAuthorInfo(blogInfo.getBlogId());

        // 校验用户权限
        if(userId != authorInfo.getUserId()) {
            log.error("当前用户无权限操作");
            return Result.fail("当前用户无权限操作,请联系管理员",false);
        }

        // 更新博客
        Integer result = blogService.updateBlog(blogInfo);
        if(result <= 0) {
            return Result.fail("更新失败,请联系管理员",false);
        }
        return Result.success(true);
    }
    @RequestMapping("/delete")
    public Result delete(Integer blogId,HttpServletRequest request) {
        // 获取 登入用户id
        String token = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserIdFromToken(token);
        // 获取作者id
        UserInfo authorInfo = userInfoService.getAuthorInfo(blogId);

        // 鉴权
        if(userId != authorInfo.getUserId()) {
            return Result.fail("当前用户无权限操作,请联系管理员",false);
        }
        // 删除博客
        Integer result = blogService.deleteBlod(blogId);
        if(result <= 0) {
            return Result.fail("删除错误,请联系管理员",false);
        }


        return Result.success(true);
    }
}
